import 'package:flutter/material.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/entrys/rq/move_library_bean.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/utils/toast_utils.dart';
import 'package:xiongan_app_yy/widgets/comm/dropdownmenu.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';

//移库作业
class MoveLibrary extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MoveLibraryState();
  }
}

class MoveLibraryState extends State<MoveLibrary> {
  bool visibility = true; //false不隐藏，true隐藏
  String selectVal;
  String selectFrame;
  String selectPosition;
  bool load = true;
  dynamic item;
  List<dynamic> libraryData = new List();
  List<dynamic> selectFrameData = new List();
  List<dynamic> selectPositionData = new List();
  String worktoolcode;
  int type;
  String total;
  Color loadBgColor = Colors.white;
  @override
  void initState() {
    super.initState();
    getLibrary();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: new AppBar(
          leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
          backgroundColor: Color(0XFF00A5F1),
          title: Text(
            "移库作业",
            style: TextStyle(fontSize: 18),
          ),
          elevation: 0,
          centerTitle: true,
        ),
        body: LoadBuilder(
          bgColor: loadBgColor,
          visibility: load,
          builder: Column(
            children: <Widget>[
              Expanded(
                child: Container(
                  padding: EdgeInsets.all(15),
                  child: Column(
                    children: <Widget>[
                      DropDownMenu(
                        hitText: "选择移出库区",
                        value: selectVal,
                        datas: libraryData,
                        keyItem: {"id": "text"},
                        //datas: libraryData,
                        onChange: (data) {
                          setState(() {
                            selectVal = data["value"];
                            item=data["item"];
                            loadBgColor = Color(0x66BCBCBC);
                            load = true;
                            visibility = true;
                          });
                          print(selectVal);
                          getFrame();
                        },
                      ),
                      DropDownMenu(
                        hitText: "选择移出货架",
                        value: selectFrame,
                        datas: selectFrameData,
                        keyItem: {"id": "text"},
                        onChange: (data) {
                          setState(() {
                            selectFrame = data["value"];
                            loadBgColor = Color(0x66BCBCBC);
                            load = true;
                            visibility = true;
                          });
                          print(selectFrame);
                          getPosition();
                        },
                      ),
                      DropDownMenu(
                        hitText: "选择移出货位",
                        value: selectPosition,
                        datas: selectPositionData,
                          keyItem: {"id": "value"},
                        onChange: (data) {
                          setState(() {
                            selectPosition = data["value"];
                            visibility = true;
                          });
                          print(selectPosition);
                        },
                      ),
                      Container(
                          width: double.infinity,
                          padding: EdgeInsets.only(
                              top: 10, bottom: 30, right: 130, left: 130),
                          child: FlatButton(
                            color: Color(0XFF00A5F1),
                            child: Text(
                              "查询",
                              style: TextStyle(color: Colors.white),
                            ),
                            onPressed: () {
                              if(selectVal!=null&&selectFrame!=null&&selectPosition!=null){
                                setState(() {
                                  load = true;
                                });
                                getInfo();
                              }else{
                                TostUtil.showToast("请选择至库位");
                              }
                            },
                          )),
                      Offstage(
                        offstage: visibility,
                        child: Container(
                          width: 330,
                          height: 98,
                          child: Card(
                              elevation: 4.0, //阴影
                              color: Colors.white, //背景色
                              child: Container(
                                padding: EdgeInsets.all(10),
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    Row(
                                      children: <Widget>[
                                        Expanded(
                                          child: Text("移出托盘码"),
                                        ),
                                        Text(worktoolcode??"空"),
                                      ],
                                    ),
                                    Row(
                                      children: <Widget>[
                                        Expanded(
                                          child: Text("物资种数"),
                                        ),
                                        Text(type.toString()??""),
                                      ],
                                    ),
                                    Row(
                                      children: <Widget>[
                                        Expanded(
                                          child: Text("物资总数"),
                                        ),
                                        Text(total??""),
                                      ],
                                    ),
                                  ],
                                ),
                              )),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              Container(
                width: double.infinity,
                height: 48,
                child: FlatButton(
                  color: Color(0XFF00A5F1),
                  child: Text(
                    "选择移入货位",
                    style: TextStyle(color: Colors.white),
                  ),
                  onPressed: () {
                    if(!visibility){
                      Navigator.pushNamed(context, "/move_in_select").then((a){
                       if(a=="ok"){
                         setState(() {
                           visibility=true;
                         });
                       }
                      });
                    }else{
                      TostUtil.showToast("没有可移动托盘");
                    }
                  },
                ),
              ),
            ],
          ),
        ));
  }

  void getLibrary() {
    InternetAPI.getInstance().get("Facilities/PrWarehouseArea/GetAreaTree",
        (data) {
      print(data);
      setState(() {
        libraryData = data["dropdownList"];
        load = false;
      });
    }, errorCallBack: (err) {}, params: {
      'operaterUserId': DataSingleton().Data.id,
      'tenantId': DataSingleton().Data.tenantId,
      'Warehouseid': DataSingleton().warehouseChoice.id,
      'pageNo': 1,
      'pageSize': 400
    });
  }

  void getFrame() {
    InternetAPI.getInstance()
        .get("Facilities/PrWarehouseShelf/GetWarehouseShelfSelectedItemList",
            (data) {
      print(data);
      setState(() {
        selectFrame = null;
        selectPosition = null;
        selectPositionData.clear();
        selectFrameData = data;
        load = false;
      });
    }, errorCallBack: (err) {
      setState(() {
        load = false;
      });
    }, params: {
      'operaterUserId': DataSingleton().Data.id,
      'tenantId': DataSingleton().Data.tenantId,
      'Warehouseid': DataSingleton().warehouseChoice.id,
      "areaid": selectVal,
        'pageNo': 1,
        'pageSize': 1000
    });
  }

  void getPosition() {
    InternetAPI.getInstance()
        .get("Facilities/PrWarehouseLocation/GetWarehouseLocationSelectedItemList",
            (data) {
          print(data);
          setState(() {
            selectPosition = null;
            selectPositionData=data;
            load = false;
          });
        }, errorCallBack: (err) {
          setState(() {

            load = false;
          });
        }, params: {
          'operaterUserId': DataSingleton().Data.id,
          'tenantId': DataSingleton().Data.tenantId,
          'Warehouseid': DataSingleton().warehouseChoice.id,
          'areaid': selectVal,
          'shelfid':selectFrame,
          'pageNo': 1,
          'pageSize': 1000
        });
  }

  void getInfo() {
    InternetAPI.getInstance()
        .get("IMS/MiInventoryItem/GetAllAvailableInventoryItem",
            (data) {
          print(data);
          double t=0;
          if(data==null || data.length == 0){
            setState(() {
              load = false;
              TostUtil.showToast("此库位空闲无物资");
            });
          }else{
            for(int i=0;i<data.length;i++){
              t=t+(data.elementAt(i)["availablecount"]??0);
            }
            setState(() {
              worktoolcode=data.elementAt(0)["worktoolcode"];
              type=data.length;
              total=t.toInt().toString();
              load = false;
              visibility = false;
            });
            MoveLibraryBean bean =new MoveLibraryBean();
            bean.reservoirAreaID=selectVal;
            bean.ShelfID=selectFrame;
            bean.cargoLocationID=selectPosition;
            bean.item=item;
            DataSingleton().moveLibraryInfo=bean;
            DataSingleton().moveLibraryInfo.bean=data;
          }

        }, errorCallBack: (err) {
          setState(() {
            load = false;
          });
        }, params: {
          'operaterUserId': DataSingleton().Data.id,
          'tenantId': DataSingleton().Data.tenantId,
          'Warehouseid': DataSingleton().warehouseChoice.id,
          'areaid': selectVal,
          'shelfid':selectFrame,
          'locationid': selectPosition,
        });
  }
}
